<?php

namespace App;

use App\Traits\Favoritable;
use App\Traits\RecordsActivity;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

/**
 * App\Reply
 *
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Favorite[] $favorites
 * @property-read mixed                                                    $favorites_count
 * @property-read mixed                                                    $is_favorited
 * @property-read \App\User                                                $owner
 * @property-read \App\Thread                                              $thread
 * @mixin \Eloquent
 * @property int                                                           $id
 * @property int                                                           $user_id
 * @property int                                                           $thread_id
 * @property string                                                        $body
 * @property \Carbon\Carbon|null                                           $created_at
 * @property \Carbon\Carbon|null                                           $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Reply whereBody($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Reply whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Reply whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Reply whereThreadId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Reply whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Reply whereUserId($value)
 */
class Reply extends Model
{
    use Favoritable, RecordsActivity;

    protected $guarded = [];
    protected $with    = ['owner', 'favorites'];
    protected $appends = ['favoritesCount', 'isFavorited', 'isBest'];

    protected static function boot()
    {
        parent::boot();
        static::created(function ($reply) {
            $reply->thread->increment('replies_count');
        });
        static::deleted(function ($reply) {
            //监听删除回复时如果是最佳回复就将最佳回复设置为 NULL,也可以的使用数据库外键的形式约束
            if ($reply->id == $reply->thread->best_reply_id) {
                $reply->thread->update(['best_reply_id' => null]);
            }
            $reply->thread->decrement('replies_count');
        });
    }

    public function getIsBestAttribute()
    {
        return $this->isBest();
    }

    public function setBodyAttribute($body)
    {
        $this->attributes['body'] = preg_replace('/@([\w\-]+)/', '<a href="/profiles/$1">$0</a>', $body);
    }

    public function owner()
    {
        return $this->belongsTo(User::class, 'user_id');  // 使用 user_id 字段进行模型关联
    }

    public function thread()
    {
        return $this->belongsTo(Thread::class);
    }

    public function wasJustPublished()
    {
        return $this->created_at->gt(Carbon::now()->subMinute());
    }

    /**
     * @的用户数组
     * @return mixed
     */
    public function mentionedUsers()
    {
        preg_match_all('/@([\w\-]+)/', $this->body, $matches);

        return $matches[1];
    }


    public function path()
    {
        return $this->thread->path() . "#reply-{$this->id}";
    }

    public function isBest()
    {
        return $this->thread->best_reply_id == $this->id;
    }

}
